package com.sojson.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sojson.dao.ArticleCateDao;
import com.sojson.entity.po.ArticleCate;
import com.sojson.service.ArticleCateService;
import com.sojson.util.file.FileDepotUtil;
import com.sojson.util.file.constant.ConstantFileFieldName;
import com.sojson.util.file.enums.EFileTable;

/**
 * 文章分类表Service实现类
 * 
 * @author liu
 * @date 2021-03-16
 */
@Service
public class ArticleCateServiceImpl implements ArticleCateService {

    @Resource
    private ArticleCateDao articleCateDaoImpl;

    /**
     * 单条插入
     * 
     * @param articleCate
     * @return
     * @throws SQLException
     */
    @Override
    public int insert(ArticleCate articleCate) {
        int insert = articleCateDaoImpl.insert(articleCate);

        if (insert > 0) {
            FileDepotUtil.insertSync(EFileTable.ARTICLE_CATE, ConstantFileFieldName.THUMB, articleCate.getThumb());
        }
        return insert;
    }

    /**
     * 批量插入
     * 
     * @param articleCate
     * @return
     * @throws SQLException
     */
    @Override
    public int insertList(ArticleCate articleCate) {
        int insertList = articleCateDaoImpl.insertList(articleCate.getArticleCates());
        return insertList;
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param articleCate
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteByDto(ArticleCate articleCate) {
        int deleteByDto = articleCateDaoImpl.deleteByDto(articleCate);
        return deleteByDto;
    }

    /**
     * 统计全部数量
     * 
     * @return
     * @throws SQLException
     */
    @Override
    public int countAll() {
        int countAll = articleCateDaoImpl.countAll();
        return countAll;
    }

    /**
     * 查询所有
     * 
     * @return
     * @throws SQLException
     */
    @Override
    public List<ArticleCate> findAll() {
        List<ArticleCate> findAll = articleCateDaoImpl.listAll();
        return findAll;
    }

    /**
     * 根据对象统计数量
     * 
     * @param articleCate
     * @return
     * @throws SQLException
     */
    @Override
    public int countByDto(ArticleCate articleCate) {
        int countByDto = articleCateDaoImpl.countByDto(articleCate);
        return countByDto;
    }

    /**
     * 根据对象查询
     * 
     * @param articleCate
     * @return
     * @throws SQLException
     */
    @Override
    public List<ArticleCate> findByDto(ArticleCate articleCate) {
        List<ArticleCate> findByDto = articleCateDaoImpl.listByDto(articleCate);
        return findByDto;
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteById(Integer id) {
        int deleteById = articleCateDaoImpl.deleteById(id);
        return deleteById;
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteByIdList(ArrayList<Integer> ids) {
        List<ArticleCate> findByIdList = articleCateDaoImpl.findByIdList(ids);

        int deleteByIdList = articleCateDaoImpl.deleteByIdList(ids);

        if (deleteByIdList > 0) {
            List<String> orderNoList = findByIdList.stream().map(ArticleCate::getThumb).collect(Collectors.toList());
            FileDepotUtil.deleteListSync(orderNoList);
        }
        return deleteByIdList;
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param articleCate
     * @return
     * @throws SQLException
     */
    @Override
    public int updateById(ArticleCate articleCate) {
        int updateById = articleCateDaoImpl.updateById(articleCate);
        return updateById;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param articleCate
     * @return
     * @throws SQLException
     */
    @Override
    public int updateStaticByIdList(ArticleCate articleCate) {
        int updateStaticByIdList = articleCateDaoImpl.updateStaticByIdList(articleCate);
        return updateStaticByIdList;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param articleCate
     * @return
     * @throws SQLException
     */
    @Override
    public int updateChangeByIdList(ArticleCate articleCate) {
        int updateChangeByIdList = articleCateDaoImpl.updateChangeByIdList(articleCate.getArticleCates());
        return updateChangeByIdList;
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param articleCate
     * @return
     * @throws SQLException
     */
    @Override
    public int updatePartialById(ArticleCate articleCate) {
        ArticleCate findById = articleCateDaoImpl.findById(articleCate.getId());

        int updatePartialById = articleCateDaoImpl.updatePartialById(articleCate);

        if (updatePartialById > 0) {
            FileDepotUtil.updateSync(EFileTable.ARTICLE_CATE.getCode(), ConstantFileFieldName.THUMB,
                articleCate.getThumb(), findById.getThumb());
        }
        return updatePartialById;
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param articleCate
     * @return
     * @throws SQLException
     */
    @Override
    public int updateStaticPartialByIdList(ArticleCate articleCate) {
        int updateStaticPartialByIdList = articleCateDaoImpl.updateStaticPartialByIdList(articleCate);
        return updateStaticPartialByIdList;
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param articleCate
     * @return
     * @throws SQLException
     */
    @Override
    public int updateChangePartialByIdList(ArticleCate articleCate) {
        int updateChangePartialByIdList = articleCateDaoImpl.updateChangePartialByIdList(articleCate.getArticleCates());
        return updateChangePartialByIdList;
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public ArticleCate findById(Integer id) {
        ArticleCate findById = articleCateDaoImpl.findById(id);
        return findById;
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    @Override
    public ArrayList<ArticleCate> findByIdList(ArrayList<Integer> ids) {
        ArrayList<ArticleCate> findByIdList = articleCateDaoImpl.findByIdList(ids);
        return findByIdList;
    }

    /**
     * 通过对象查询id列表
     * 
     * @param articleCate
     * @return
     * @throws SQLException
     */
    @Override
    public ArrayList<Integer> findIdByDto(ArticleCate articleCate) {
        ArrayList<Integer> findIdByDto = articleCateDaoImpl.findIdByDto(articleCate);
        return findIdByDto;
    }

    /**
     * 清空数据(物理清空)
     * 
     * @throws SQLException
     */
    @Override
    public void clear() {
        articleCateDaoImpl.clear();
    }

}